package com.ojoin.trade.supplyloan_core.web.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ojoin.trade.common.utils.TimeUtils;
import com.ojoin.trade.supplyloan_core.web.domain.*;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import com.ojoin.trade.common.exception.BusinessException;
import com.ojoin.trade.common.exception.DaoException;
import com.ojoin.trade.common.web.dao.impl.BaseDaoImpl;
import com.ojoin.trade.supplyloan_core.web.dao.TbSlLoanApplicationDao;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 15-10-26
 * Time: 上午11:24
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class TbSlLoabApplicationDaoImpl extends BaseDaoImpl<TbSlLoanApplication> implements TbSlLoanApplicationDao {
 
 
    
    @Override
    public List<TbSlLoanApplicationInfoVO> queryAllTbSlApplication() throws BusinessException {

        String mapMethodName = "selectAllApplication";
        try { 
            List list = this.sqlSessionTemplate.selectList(getSqlName(mapMethodName));
            if (list != null && list.size() >0)
              return list ;
        } catch (Exception e) {
            throw new DaoException(String.format("查询用户申请供应链贷款记录出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }
        return null;
    }

    @Override
    public TbSlLoanApplication queryTbSlApplicationByJoinId(int id_of_tb_merchant_user) throws BusinessException {
        String mapMethodName = "selectByCause";
        Map<String,Object> paramap=new HashMap<String, Object>();
        paramap.put("idOfTbMerchantUser",id_of_tb_merchant_user);
        try {
            List list = this.sqlSessionTemplate.selectList(getSqlName(mapMethodName),
                    paramap);
            if (list != null && list.size() >0)
                return (TbSlLoanApplication) list.get(0);
        } catch (Exception e) {
            throw new DaoException(String.format("查询用户申请贷款记录->" + id_of_tb_merchant_user + "出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }

        return null;
    }
    
    @Override
    public void update(Map<String, Object> parammap) throws BusinessException {
        String mapMethodName = "update";
        try{
            this.sqlSessionTemplate.update(getSqlName(mapMethodName),parammap);
        }catch (Exception e) {
            throw new DaoException(String.format("更新用户的申请贷款记录出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }
    }

	@Override
	public List<TbSLLoanTodoTaskVO> queryAdminUserTodoTaskList()
			throws BusinessException {
		String mapMethodName = "queryAdminUserTodoTaskList";
		try {
			return this.sqlSessionTemplate.selectList(getSqlName(mapMethodName));
		} catch (Exception e) {
			throw new DaoException(String.format("查询后台管理员代办任务列表出错！语句：%s",
					getSqlName(mapMethodName)), e);
		}
	}

	@Override
	public List<TbSLLoanTodoTaskVO> queryOperatorTodoTaskList()
			throws BusinessException {
		String mapMethodName = "queryOperatorTodoTaskList";
		try {
			return this.sqlSessionTemplate.selectList(getSqlName(mapMethodName));
		} catch (Exception e) {
			throw new DaoException(String.format("查询保理员代办任务列表出错！语句：%s",
					getSqlName(mapMethodName)), e);
		}
	}

	@Override
	public List<TbSlLoanApplicationInfoVO> querySLLoanAppInfoListByStatus(
			int loanstatus, Pageable page) throws BusinessException {
		String mapMethodName = "querySLLoanAppInfoListByStatus";
		try {
			return this.sqlSessionTemplate.selectList(getSqlName(mapMethodName), loanstatus, this.getPageBounds(page));
		} catch (Exception e) {
			throw new DaoException(String.format("根据状态" + loanstatus + "查询供应链贷申请记录出错！语句：%s",
					getSqlName(mapMethodName)), e);
		}
	}

	@Override
	public List<TbSlLoanApplicationInfoVO> queryCreditedSLLoanAppInfoList(
			int loanstatus, Pageable page) throws BusinessException {
		String mapMethodName = "queryCreditedSLLoanAppInfoList";
		try {
			return this.sqlSessionTemplate.selectList(getSqlName(mapMethodName), loanstatus, this.getPageBounds(page));
		} catch (Exception e) {
			throw new DaoException(String.format("根据状态" + loanstatus + "查询供应链贷申请记录出错！语句：%s",
					getSqlName(mapMethodName)), e);
		}
	}
	
	
	@Override
	public List<TbSlLoanApplicationStatVO> querySlLoanApplyStatListByStatus(
			int loanstatus, int joinuserid, Pageable page) throws BusinessException {
		String mapMethodName = "querySlLoanApplyStatListByStatus";
		Map<String,Object> paramap=new HashMap<String, Object>();
        paramap.put("loanstatus",loanstatus);
        paramap.put("idOfTbMerchantUserOfCreditor",joinuserid);
        
		try {
			return this.sqlSessionTemplate.selectList(getSqlName(mapMethodName), paramap, this.getPageBounds(page));
		} catch (Exception e) {
			throw new DaoException(String.format("根据状态" + loanstatus + " 保理id" + joinuserid + "查询供应链贷申请记录出错！语句：%s",
					getSqlName(mapMethodName)), e);
		}
	}
	
	@Override
	public int updateByPrimaryIdAndJoinid(TbSlLoanApplication tbsl)
			throws BusinessException {
  
		 String mapMethodName = "updateByPrimaryAndJoinid";
	        try{
	            return this.sqlSessionTemplate.update(getSqlName(mapMethodName),tbsl);
	        }catch (Exception e) {
	            throw new DaoException(String.format("更新用户的申请贷款记录出错！语句：%s",
	                    getSqlName(mapMethodName)), e);
	        }
	        
	}

	@Override
	public int updateByJoinid(TbSlLoanApplication tbsl)
			throws BusinessException {
		 String mapMethodName = "updateByJoinid";
	        try{
	            return this.sqlSessionTemplate.update(getSqlName(mapMethodName),tbsl);
	        }catch (Exception e) {
	            throw new DaoException(String.format("更新用户的申请贷款记录出错！语句：%s",
	                    getSqlName(mapMethodName)), e);
	        }
	        
	}

    @Override
    public String queryMerNameByJoinid(int id) throws BusinessException {
        String mapMethodName = "selectMerchnameByCreditid";
        try{
            return this.sqlSessionTemplate.selectOne(getSqlName(mapMethodName),id);
        }catch (Exception e) {
            throw new DaoException(String.format("查询授信公司的名称记录出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }
    }

    @Override
    public List<TbSlLoanApplicationInfoVO> queryNeedMessage() throws BusinessException {
        Map<String,Object> paramMap=new HashMap<>();
        paramMap.put("curdate", TimeUtils.getCurDate());
        String method="selectwillexpireformch";
        try{
            return this.sqlSessionTemplate.selectList(method,paramMap);
        }catch (Exception e){
            throw new DaoException(String.format("查询合同保理快要过期记录出错！语句：%s",
                    getSqlName(method)), e);
        }
    }

	@Override
	public String querySLLoanAppProcInstIdById(int id) throws BusinessException {
		String mapMethodName = "querySLLoanAppProcInstIdById";
        try{
            return this.sqlSessionTemplate.selectOne(getSqlName(mapMethodName),id);
        }catch (Exception e) {
            throw new DaoException(String.format("查询申请记录的流程实例ID出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }
	}

    @Override
    public List<TbApplicationExpireVo> queryTbApplicationExpireVo(Pageable page) throws BusinessException {
        Map<String,Object> paramMap=new HashMap<>();
        paramMap.put("curdate", TimeUtils.getCurDate());
        String method="selectwillexplireforoperator";
        try{
            return this.sqlSessionTemplate.selectList(method,paramMap,getPageBounds(page));
        }catch (Exception e){
            throw new DaoException(String.format("查询营业执照过期或者合同保理快要过期记录出错！语句：%s",
                    getSqlName(method)), e);
        }
    }


}
